package com.example.button_app;

import java.io.IOException;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialProber;

import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class MainActivity extends Activity {
	
	Button button;
	public final static String EXTRA_MESSAGE = "com.example.button_app.MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		addListenerOnButton();
	}

	public void addListenerOnButton() {
		/*button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener(){
			
			public void onClick(View arg0) {
				Intent browserIntent = 
				    new Intent (Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
				    startActivity(browserIntent);
			}
		});*/
	
		
	}
	
	public void sendMessage(View view){
		Intent intent = new Intent(this, DisplayMessageActivity.class);
		EditText editText = (EditText) findViewById(R.id.edit_message);
		String message = editText.getText().toString();
		intent.putExtra(EXTRA_MESSAGE, message);
		startActivity(intent);
		
	}
	
	private Intent Intent() {
		// TODO Auto-generated method stub
		return null;
	}

	public void sendUART(String data)
	{
		UsbManager manager = (UsbManager)getSystemService(Context.USB_SERVICE);
		UsbSerialDriver driver = UsbSerialProber.acquire(manager);
		if(driver != null){
			try {
				driver.setBaudRate(115200);
				byte buffer[] = new byte[16];
				int numByteRead = driver.read(buffer, 1000);
				Log.d("myTag", "Read " + numByteRead + " bytes.");
				
				byte temp[] = data.getBytes();
				
				driver.write(temp, 16);			//Writes a 16 element byte array to the driver?	
				} catch (IOException e){		//Reconfigure function to take an int or byte type,
					e.getCause();				//comment out unnecessary stuff and try.
					
			} finally {
				try {
					driver.close();
				} catch (IOException e){
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
